{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "-N59E9S4BzSr"
   },
   "source": [
    "##### Copyright 2020 The Cirq Developers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cellView": "form",
    "id": "_SAdH7I0B2rz"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "# https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "NZv6mLU-B_b0"
   },
   "source": [
    "# Get started with Quantum Computing Service"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "vBsLfco2CD12"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://www.example.org/cirq/tutorials/google/start\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on QuantumLib</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/master/docs/tutorials/google/start.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/master/docs/tutorials/google/start.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/tutorials/google/start.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "846b32703c5c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    import cirq\n",
    "except ImportError:\n",
    "    print(\"installing cirq...\")\n",
    "    !pip install --quiet cirq\n",
    "    print(\"installed cirq.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "So5iLqkNPnFm"
   },
   "source": [
    "Quantum Computing Service enables researchers to run quantum programs on Google's quantum processors. This notebook is a tutorial to get you started with the typical setup, using the open source Python framework [Cirq](https://github.com/quantumlib/Cirq), in the free cloud Jupyter notebook environment, [Google Colab](https://colab.research.google.com/).\n",
    "\n",
    "Access is currently restricted to those in an approved group, and you must be in that group before running this tutorial.\n",
    "\n",
    "You can find more about running this in colaboratory in the [Colab documentation](https://colab.research.google.com/notebooks/welcome.ipynb) or in our Cirq-specific guide to [running in Colab](colab.ipynb).  You can download this notebook from the [GitHub repository](https://github.com/quantumlib/Cirq/blob/master/docs/tutorials/google/start.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "da8NOLsZqLsU"
   },
   "source": [
    "## Before you begin\n",
    "\n",
    "*  First, decide which project you will use the Quantum Computing Services from.  All of your quantum programs and results will live under a project which you specify when creating and running these programs using Quantum Engine.  You can use an existing project or create a new project.  [Learn more about creating a project](https://cloud.google.com/docs/overview/).\n",
    "*  Log in and agree to [Terms of Service](https://cloud.google.com/terms/).\n",
    "*  Follow this link to [**enable the Quantum Engine API**](https://console.cloud.google.com/apis/library/quantum.googleapis.com?returnUrl=quantum) in your Google Cloud Platform project.\n",
    "\n",
    "After the API is enabled, you should be redirected to the [Quantum Engine console](https://console.cloud.google.com/quantum) and it should look like the following screenshot.\n",
    "\n",
    "![Quantum Engine landing](https://raw.githubusercontent.com/quantumlib/Cirq/master/docs/images/console-landing.png)\n",
    "\n",
    "**Enter your project id into the input text box below**. To find your project id, click on the project menu in the blue bar at the top of the console. This will open a menu that displays your project name (e.g. \"My project\") and unique **project id** (e.g. my-project-1234). Enter the **project id** into the input below. ([Help on finding your project id](https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects).)\n",
    "\n",
    "![Find project id](https://raw.githubusercontent.com/quantumlib/Cirq/master/docs/images/console-project-id.png)\n",
    "\n",
    "**Run the code in the next block (the one with the text box)**, which will prompt you to authenticate Google Cloud SDK to use your project.  You can run the code by either clicking the play button (pointed by arrow below) or by selecting the block and pressing CTRL-ENTER.  After running the block you will see a link which you should click.  This will open a new browser window.  Follow the authentication flow for this window.  After you authenticate and allow access to your project, you will be given a string which you should enter into the text box that appears in the run area (and then press return).  If you see \"Authentication complete\" you have done this step successfully. If this fails, make sure that you have cut and paste the string correctly (e.g. the clipboard button seems to not work for some browser/OS combinations). \n",
    "\n",
    "![Run code block below](https://raw.githubusercontent.com/quantumlib/Cirq/master/docs/images/run-code-block.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "cellView": "form",
    "id": "YoqI9GrOPExP"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Notebook is not executed with Colab, assuming Application Default Credentials are setup.\n",
      "Successful authentication to Google Cloud.\n"
     ]
    }
   ],
   "source": [
    "# The Google Cloud Project id to use.\n",
    "project_id = '' #@param {type:\"string\"}\n",
    "\n",
    "if project_id == '':\n",
    "    import os \n",
    "    if 'GOOGLE_CLOUD_PROJECT' not in os.environ:\n",
    "        raise Exception(\"Please setup project_id in this cell or set the `GOOGLE_CLOUD_PROJECT` env var to your project id.\")\n",
    "    project_id = os.environ['GOOGLE_CLOUD_PROJECT']\n",
    "else: \n",
    "    import os\n",
    "    os.environ['GOOGLE_CLOUD_PROJECT'] = project_id\n",
    "\n",
    "def authenticate_user():\n",
    "  \"\"\"Runs the user through the Colab OAuth process.\n",
    "  \n",
    "  Checks for Google Application Default Credentials and runs interactive login \n",
    "  if the notebook is executed in Colab. In case the notebook is executed in Jupyter notebook\n",
    "  or other IPython runtimes, no interactive login is provided, it is assumed that the \n",
    "  `GOOGLE_APPLICATION_CREDENTIALS` env var is set or `gcloud auth application-default login`\n",
    "  was executed already.\n",
    "  \n",
    "  For more information on using Application Default Credentials see \n",
    "  https://cloud.google.com/docs/authentication/production\n",
    "  \"\"\"\n",
    "  in_colab = False\n",
    "  try:\n",
    "     from IPython import get_ipython\n",
    "     in_colab = 'google.colab' in str(get_ipython())\n",
    "  except: \n",
    "     # Notebook is not executed within IPython. Assuming external authentication.\n",
    "     return \n",
    "\n",
    "  if in_colab: \n",
    "      from google.colab import auth      \n",
    "      print(\"Getting OAuth2 credentials.\")\n",
    "      print(\"Press enter after entering the verification code.\")\n",
    "      auth.authenticate_user(clear_output=False)\n",
    "      print(\"Authentication complete.\")\n",
    "  else: \n",
    "      print(\"Notebook is not executed with Colab, assuming Application Default Credentials are setup.\") \n",
    "\n",
    "authenticate_user()\n",
    "\n",
    "print(\"Successful authentication to Google Cloud.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ocy3JossWU57"
   },
   "source": [
    "**Authentication details** Double clicking on the project_id block above should expose the code that is run when you run this code block.  This code uses the [colabtools](https://github.com/googlecolab/colabtools/blob/master/google/colab/auth.py) auth module to ensure that *Application Default Credentials* are set and then creates a variable `colab_auth` which can be used in Cirq to authenticate your calls to Quantum Computing Service.\n",
    "\n",
    "If you are going to run code outside of colab and want to authenticate, see the below section on running from the command-line."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "47n3bsx3Nl9g"
   },
   "source": [
    "## Create a circuit\n",
    "Now that you've enabled Quantum Computing Service and configured the notebook, let's create a basic program with Cirq. After reviewing the code, **run this block** to run a circuit, and print a circuit diagram and results. To learn more, refer to the [Intro to Cirq](../basics.ipynb) and [Cirq documentation](https://cirq.readthedocs.io)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "cellView": "both",
    "id": "EQoTYZIEPa9S"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Circuit:\n",
      "(0, 0): ───X───M('result')───\n"
     ]
    }
   ],
   "source": [
    "import cirq\n",
    "import cirq.google as cg\n",
    "import random\n",
    "import string\n",
    "\n",
    "# Define a qubit at an arbitrary grid location.\n",
    "qubit = cirq.GridQubit(0, 0)\n",
    "\n",
    "# Create a circuit (qubits start in the |0> state).\n",
    "circuit = cirq.Circuit(\n",
    "    cirq.X(qubit),                     # NOT gate.\n",
    "    cirq.measure(qubit, key='result')  # Measurement.\n",
    ")\n",
    "\n",
    "print(\"Circuit:\")\n",
    "print(circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BJIWCdhKgVo7"
   },
   "source": [
    "## Simulate the circuit using Cirq\n",
    "\n",
    "Let's quickly use Cirq to simulate the circuit above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "TW_zU_pagVP0"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Simulating circuit using Cirq...\n",
      "\n",
      "Measurement results:\n",
      "result=1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n"
     ]
    }
   ],
   "source": [
    "# Simulate the circuit, repeating 1000 times.\n",
    "print(\"Simulating circuit using Cirq...\\n\")\n",
    "results = cirq.sample(circuit, repetitions=1000)\n",
    "print(\"Measurement results:\")\n",
    "print(results)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "4ZK4GQWmyo-u"
   },
   "source": [
    "## Run on quantum hardware\n",
    "Approved users can access quantum hardware in two modes. First, all approved users have access to a processor in \"open-swim\" which is a first-in-first-out queue with fairness algorithm that balances jobs across users in the queue. Secondly, processors can be reserved in hourly blocks if the user is approved.  You can learn more about the reservation system on the [concepts page](../../google/engine.md). We'll use the processor `pacific` in this demo."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "uz0xSlml3fkA"
   },
   "source": [
    "### Create a Quantum Engine client\n",
    "Interactions with hardware are facilitated by the Quantum Computing Service. A client must first be initialized with your Google Cloud project to perform these interactions. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "id": "V40sPIi63f0I"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# Create an Engine client.\n",
    "engine = cirq.google.get_engine()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "FS42e_sx0Hqw"
   },
   "source": [
    "### View the processor's topology\n",
    "Each processor has a set of available qubits laid out on a grid with limited couplings between qubits. The device specification can be printed to inspect the topology of a processor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "O-Jrib9y1TFY"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                                             (0, 5)───(0, 6)\n",
      "                                             │        │\n",
      "                                             │        │\n",
      "                                             (1, 5)───(1, 6)───(1, 7)\n",
      "                                             │        │        │\n",
      "                                             │        │        │\n",
      "                                    (2, 4)───(2, 5)───(2, 6)───(2, 7)───(2, 8)\n",
      "                                             │        │        │        │\n",
      "                                             │        │        │        │\n",
      "                                             (3, 5)───(3, 6)───(3, 7)───(3, 8)───(3, 9)\n",
      "                                             │        │        │\n",
      "                                             │        │        │\n",
      "         (4, 1)───(4, 2)            (4, 4)───(4, 5)───(4, 6)───(4, 7)\n",
      "         │        │                 │        │        │        │\n",
      "         │        │                 │        │        │        │\n",
      "(5, 0)───(5, 1)───(5, 2)───(5, 3)───(5, 4)───(5, 5)───(5, 6)───(5, 7)\n",
      "         │        │        │        │        │        │        │\n",
      "         │        │        │        │        │        │        │\n",
      "         (6, 1)───(6, 2)───(6, 3)───(6, 4)───(6, 5)───(6, 6)───(6, 7)\n",
      "                  │        │        │        │        │\n",
      "                  │        │        │        │        │\n",
      "                  (7, 2)───(7, 3)───(7, 4)───(7, 5)───(7, 6)\n",
      "                           │        │        │\n",
      "                           │        │        │\n",
      "                           (8, 3)───(8, 4)───(8, 5)\n",
      "                                    │\n",
      "                                    │\n",
      "                                    (9, 4)\n"
     ]
    }
   ],
   "source": [
    "processor = engine.get_processor('pacific')\n",
    "\n",
    "# Print the device showing qubit connectivity for the Sycamore gateset.\n",
    "device = processor.get_device([cg.SYC_GATESET])\n",
    "print(device)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "bJ7ePCCNrh82"
   },
   "source": [
    "Note that the qubit that we used for the simulation above, `(0, 0)`, does not exist on the hardware. Since the grid may change over time, we'll programatically select a valid qubit by inspecting `device.qubits`. We then use the `optimized_for_sycamore()` function to remap the circuit description for our device; this function will also substitute gates to comply with the device's gate set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "id": "ma8JDhTUR389"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 2): ───PhXZ(a=-1,x=1,z=0)───M('result')───\n"
     ]
    }
   ],
   "source": [
    "valid_qubit = device.qubits[0]\n",
    "\n",
    "# Re-map qubits and gates for Sycamore hardware.\n",
    "syc_circuit = cg.optimized_for_sycamore(\n",
    "    circuit,\n",
    "    new_device=device,\n",
    "    qubit_map=lambda q: valid_qubit) # Constant fn since there's only one qubit.\n",
    "\n",
    "print(syc_circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TH4vtUeq2ryV"
   },
   "source": [
    "### Create a job on the Quantum Engine\n",
    "\n",
    "Cirq circuits are represented in the Quantum Computing Service as _Programs_. To run a _Program_, you must create a _Job_ that specifies details about the execution, _e.g._ the processor to use and the number of times to repeat the experiment. This enables a single circuit to be run multiple times in different configurations. For a one-off use, these steps can be done together by using the `engine.run_sweep` utility to create both a _Program_ and a _Job_.\n",
    "\n",
    "A new _Job_ will be scheduled on the requested hardware as it becomes available. The execution of your _Job_ will likely be completed within a few seconds and the results will be displayed below. The output will include a link to the [console](https://console.cloud.google.com/quantum), where you can view the status and results of your jobs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "-a0I0cbGyivS"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uploading program and scheduling job on the Quantum Engine...\n",
      "\n",
      "Scheduled. View the job at: https://console.cloud.google.com/quantum/programs/prog-UEPDBU3CSSULX8IW200829?&project=quantum-cloud-client\n",
      "Measurement results:\n",
      "\n",
      "1111111111111111101111111111111111110111101111111111111111111111111111111111111111111111111111111111111111111111111111111101101111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111101111111111111111111111110111011111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111101111111111111111111111111111111111111111111110111111111101111111111111111111111111111111111111101111101111111111111111011111111111111111111011111111111111111111111110111101111111111111110111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111101111111111111101111111111111011111101111111111110111111011111111111111111111101111111111111111111111111111110111111111111111111111111111111110111111111111111011011111111111111111111111111111111111111111101111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111101111111111111111111110111111101111111111111111111111111111110111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111110011111111111111111111101111111111111111111111101011111111011010111111111111111111011110111111111111111111111111111011111111111111111011111011111111111111011111111111111111111111111111111111111111110110111111111111111111011111111111111111111111111111111111101111111111111111110111110111111101111111111111111101111111111111111110111111111111101111111111111110101111111111111111111111111111111111111111111111111111111001111111111111111111110111111111111111111111111111111110111011111111111111111111111111101111111111111111111111111111101111111110111110111111111111101111111111111111111111111111111101111111111111111111111111111110101111111111111111111111011111111111110111111111111111111111011111111111111111111111111110111111111111111111111111111111011111111111111111011111111111111111011111111110111111111111111111111011111111110111111111111111111111111111110011111111111111111111111111111111111111111101111111111111111111111111111111111101111111111111111111111111111111111111111111111010111111111111111111111111111111111111111111111111111111111111111111111111111111111101101111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111101111111101111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111011111111111111111111111111111111101111111111111111111111110111111111111011111111111011101110111111111111101111111111111101111111011111010111111111110111111111111111111111101011111111111111111111111111111111111011111111111111111101111111111111111110111111011111111110011111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111101111111111101111111111111111111011110111111111111111110111110111110111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111101111111111011111111111111110111111111111111111111111111111111111111111111111101111111111111111111111111111111011111111111111111111111111110111111111111101001111111111111110111111111111111111111111111111111111111111111111111111101111111111111111111111111011111111111101111111111111111111111111111111111111111111111111111111110111111111011011111111111110111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111011111111111111111111111111111111110111111101111111111111111011111111111111111111111111111111111111111111111111111111111111111110111111111110111011111111111111111111111111111111111111111111111111111111111111110111111111111111111101111111111111111111111111111111111111111111111111111111111011101111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111011111100111111111101111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111001101110111111111101111111111111110111111111111111111111111110111111111111111111111111111111111011111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111011111111111111111111011111111111111111111111111111111101111011111111111111111111111101111111111111111111111011111111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111011111111111110111111111110101111111111101111111111111111111111111111111111111111111111111110111110111111110111111111111111111111111111101101111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111011101111111101011111111111111011111011111111111111111110111011111111101111111111111111111111111111011111111111111111111111111111111111101111111111111110111111111111111111111101111111111111111111111110111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111111111111111111111111111111111111111111101111111111111111111111111111111111110101111111011111111111111111111111111111111111111111111111101111111111111111111111011111101111111111111011111111111111111111111111111111111111111111111111111111111111101111111111111111101111111111111111111011111110111111111111110111111111111111111111111111111011110111111111111111111011111111111111111111111111111111101111111111111110111111100110111111111111111111111111111111111111111111111111111111111111111101011111111111111111111110111111110111111111111111111111111111111111111111111111111110111111111111111111111011111111111111011111101111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111111111111111111101111111111111111111101111111111111111111111011111111101111111111111111111111111111111111111101111111010111111111111111111111111111111111111111101111111111111111111111011111111111111111111111111111111111111111111101111111111011111011111111111111111111111111111111111111111111110111111111101111111111111111111111111111111111111111111111111111111011111111111111111111111011111010111111111111111111111011111111111111111111111110111111111111111111111111111111111111111101111111111111100111111111011111111111111111111111101110111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111110111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111100111111111111111111111111111101011111111111111111111111111111111101111111111111111111111111111111110101111111111111111111111111110111111111111111111111111111111111011111111111111111111111111111110111111111111111111111111111111111111111010111111111111111111111111111011111111111111111111111111111011111111111111111111011111111111111111111111111111110111101111111111111111111111111111111111111111111111011111111111111111111111111111111111101111111111111111101110111111111111101111111111111111111111111111111111111111111111111111111111111110111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111001111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111101111111111111111111111111111111111011111101111111111111111111111111111111111111111111111111111101111111100111011111111111111111111111011111111011111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111101111111111111111111111011111111111111111111101111011111111111111111111111110111111111111111111111111111111111111111111111011110111111101111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111110111011111111111111111111111111101111111111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111111111111111111111111111011111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111101111111111111111111111111111111111111111101101111111111101111101110111011111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111101111111111111111111111111111011011111111111111111111111111111101110111111111101111111110111111111111111111101111111111111111111111111111111111111111111111011111111111111101111111111111111111111111110111011111011111111111011111111111110111111111111111111111111111111111111111111111111111011111111111111111111011111101101111111111011111111111111011111111111111111111111111111111111111111111110111101111011111111111011111111111111111111111111011111011111111111111111110111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111111111111111111111111111101111111111111111110111111111111111111111100111111111111111111111111111111111011111111111110111111111111111111111111011111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111111111110111111111011111111111111111111111111111111111111111111111111111111111111111111111111011111111111111111111111111111111110111111111111111111111111111111111111101111110111111111111101111111011111011111111111111111111110111111111111111011111111111111011011111111110111111111111111111111111111101101111011111111111111111111111111111111111111111101111111111111111111111101111111110011110111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110111111111111111111111101111111111111111111111111111111111111111111111111111111111111011111111111111111111111111011111111111111111111111111111111111101111111111111011111111111111111011101111111111\n"
     ]
    }
   ],
   "source": [
    "print(\"Uploading program and scheduling job on the Quantum Engine...\\n\")\n",
    "\n",
    "# Upload the program and submit jobs to run in one call.\n",
    "job = engine.run_sweep(\n",
    "    program=syc_circuit,\n",
    "    repetitions=10000,\n",
    "    processor_ids=[processor.processor_id],\n",
    "    gate_set=cg.SYC_GATESET)\n",
    "\n",
    "print(\"Scheduled. View the job at: https://console.cloud.google.com/quantum/\"\n",
    "      \"programs/{}?&project={}\".format(job.program_id, project_id))\n",
    "\n",
    "# Print out the results. This blocks until the results are returned.\n",
    "results = [str(int(b)) for b in job.results()[0].measurements['result'][:, 0]]\n",
    "print(\"Measurement results:\\n\")\n",
    "print(''.join(results))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3u4dLfDugI9l"
   },
   "source": [
    "## Running from the command line\n",
    "\n",
    "If you are planning to access Quantum Computing Service from the command line, follow these instructions to get started.  If you plan on executing all of your programs from an ipython notebook, you can skip this section.\n",
    "\n",
    "We assume that your local machine is configured to run Python 3.6 or greater.  \n",
    "\n",
    "Instructions for installing Python 3:\n",
    "\n",
    "*   Linux: https://docs.python-guide.org/starting/install3/linux/\n",
    "*   Mac: https://docs.python-guide.org/starting/install3/osx/\n",
    "*   Windows:  If you are using Windows Subsystem for Linux: https://docs.python-guide.org/starting/install3/linux/, otherwise: https://docs.python-guide.org/starting/install3/win/\n",
    "\n",
    "### Setup authentication\n",
    "\n",
    "In this quickstart we will authenticate using the gcloud command line cool.  To do this, one must first install gcloud.  Follow the instructions for this at https://cloud.google.com/sdk/install.  If you are running on a corp desktop, gcloud should be installed.  We will authenticate using Application Default Credentials.  To do this simply run the following on your shell command line\n",
    "\n",
    "`gcloud auth application-default login`\n",
    "\n",
    "This will open up a browser window or give you a link to a webpage you can navigate to in order to go through an authentication flow.  Complete this using your @google.com account.  After this command is run, credentials will be stored on your local machine.  If at any point you want to revoke these credentials you can run `gcloud auth application-default revoke`\n",
    "\n",
    "We highly recommend that you setup a virtual environment for this installation to isolate your development stack from your overall system installations.  Instructions for virtual environments are at https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/.  Make sure to setup the virtual environment for Python 3 and not Python 2."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "quSdrNseg8LC"
   },
   "source": [
    "### Write and run a short quantum program\n",
    "\n",
    "Using your favorite IDE or editor, read and then paste the following hello_qubit program into a file called `hello_qubit.py`.  Make sure to replace the `'your-project-id'` string with the project id you created above.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "id": "4RXHE070gleY"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "import cirq\n",
    "\n",
    "def example():\n",
    "    \"\"\"Hello qubit example run against a quantum processor.\"\"\"\n",
    "\n",
    "    # Define a qubit.\n",
    "    qubit = cirq.GridQubit(5, 2)\n",
    "\n",
    "    # Create a circuit (qubits start in the |0> state).\n",
    "    circuit = cirq.Circuit(\n",
    "        cirq.X(qubit)**0.5,                # Square root of NOT.\n",
    "        cirq.measure(qubit, key='result')  # Measurement.\n",
    "    )\n",
    "\n",
    "    # Create an Engine object.  This uses the project id of your\n",
    "    # Google cloud project.\n",
    "    project_id = 'your-project-id'\n",
    "    engine = cirq.google.Engine(project_id=project_id)\n",
    "\n",
    "    print(\"Uploading program and scheduling job on Quantum Engine...\\n\")\n",
    "\n",
    "    # Upload the program and submit jobs to run in one call.\n",
    "    job = engine.run_sweep(\n",
    "        program=circuit,\n",
    "        repetitions=1000,\n",
    "        processor_ids=['pacific'],\n",
    "        gate_set=cirq.google.SYC_GATESET)\n",
    "\n",
    "    print(\"Scheduled. View the job at: https://console.cloud.google.com/quantum/\"\n",
    "          f\"programs/{job.program_id}/jobs/{job.job_id}\"\n",
    "          f\"/overview?project={project_id}\")\n",
    "\n",
    "    # Print out the results. This blocks until the results are returned.\n",
    "    results = [str(int(b)) for b in job.results()[0].measurements['result'][:, 0]]\n",
    "    print(\"\\nMeasurement results:\")\n",
    "    print(''.join(results))\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    example()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TjYi3VOTgyvh"
   },
   "source": [
    "You should then be able to run this program from your command line using:\n",
    "\n",
    "`python hello_qubit.py`\n",
    "\n",
    "The output should be something like:\n",
    "\n",
    "```\n",
    "Uploading program and scheduling job on Quantum Engine...\n",
    "\n",
    "Scheduled. View the job at: https://console.cloud.google.com/quantum/programs/example-T5K9Y9/jobs/job-0?mods=quantum_ng2&project=quantum-cloud-client\n",
    "\n",
    "Measurement results:\n",
    "1010101010111011000011101010101011010011100111110001101101101011100011111010100100011000010101110010011000100001110000100010011100111101001101101010000000111101010100001001101110101010110000010011110101100000101111101000010000000100010111010101110001110101101100010001101111100110001001010000100000110110100000111111110011100101111000010110010101100010111111100010001100111011010101111011000110100001010000011100000100101010111110111010011010100011011111110110011101010001000101000011101000111000001001101000110000111111000111100001001000010101011111000111111010100001011001010011011000010110111010001011000001011110001010110100001101011101110011111100100101010011100010010001100101100001101010010011001010100011010010011000010010001010111101001000011000101111011111100111111111101001010010001010000101011101100100001101011100101010101110111001101000100101111101100000100010000101000011110110101001101100000000000111000000111011101101110000001110110001111011000100111111101110111111101001110110000110\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WCKQ1lIJSgJ7"
   },
   "source": [
    "## Next steps\n",
    "* Use [this template colab](colab.ipynb) as a base for your own explorations.\n",
    "* Explore [best practices](../../google/best_practices.md) for getting circuits to run on hardware.\n",
    "* Use the [reservation colab](reservations.ipynb) for getting circuits to run on hardware."
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "name": "start.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
